package com.sgb.capital.view.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.capital.R;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.callback.ZMessageEvent;
import com.sgb.capital.databinding.ExaminedetailsFragmentBinding;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.ui.activity.BillOrderDetailsActivity;
import com.sgb.capital.view.ui.adapter.OrderLog2Adapter;
import com.sgb.capital.viewmodel.DetailsModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/10/27 0027
 * 说明:审批日志
 */
public class ExamineDetailsFragment extends AppLazyFragment {

    private ExaminedetailsFragmentBinding mBinding;
    private DetailsModel mModel;
    private OrderLog2Adapter mAdapter;
    private BillOrderDetailsActivity mActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.examinedetails_fragment, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mModel = new ViewModelProvider(this).get(DetailsModel.class);
        mAdapter = new OrderLog2Adapter(null);
        mBinding.rv.setLayoutManager(new LinearLayoutManager(Utils.getContext()));
        mBinding.rv.setAdapter(mAdapter);
    }

    @Override
    public void initObserve() {
        mModel.mProcessLogBean.observe(this, data -> {
            if (data == null) {
                return;
            }
            mBinding.errorView.setVisibility(data.size() != 0 ? View.GONE : View.VISIBLE);
            mAdapter.setNewInstance(data);
            mAdapter.notifyDataSetChanged();
        });
    }

    @Override
    protected void onLazyLoad() {
        mActivity = (BillOrderDetailsActivity) getActivity();
        mModel.getProcessLog(mActivity, mActivity.mPayDetailBean.payApplyId);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe
    public void getCode(ZMessageEvent event) {
        switch (event.code) {
            case Constants
                    .EVENT_REFRESH_LIST:
                mModel.getProcessLog(mActivity, mActivity.mPayDetailBean.orderId);
                break;
        }
    }
}